﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace ConsoleApplication2
{
    class BackgroundWorker2222
    {

        static void Main(string[] args)
        {

            //
            int aa = 1;
            string s = "194639793114743";
            for (int i = 0; i < s.Length; i++)
            {
                aa *= Convert.ToInt32(s[i].ToString());
                
            }


            //   BackGroundWorker b;
            //ComponentModel
            BackgroundWorker back = new BackgroundWorker();
            back.WorkerReportsProgress = true;
            back.WorkerSupportsCancellation = true;
            back.DoWork += new DoWorkEventHandler(back_DoWork);
            back.ProgressChanged += new ProgressChangedEventHandler(back_ProgressChanged);
            back.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);


            BackgroundWorker back2 = new BackgroundWorker();
            back2.WorkerReportsProgress = true;
            back2.WorkerSupportsCancellation = true;
            back2.DoWork += new DoWorkEventHandler(back2_DoWork);

            Product p = new Product();
            p.Name = "3";
            if (!back.IsBusy)
            {
                back.RunWorkerAsync(p);
            }
            //if (!back2.IsBusy)
            //{
            //    back2.RunWorkerAsync(p);
            //}

            while (true)
            {
                if (!back.IsBusy)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(200);
                }
            }




            Thread.Sleep(2000);

            if (back.WorkerSupportsCancellation==true)
            {
                //back.CancelAsync();
                
            }
           
            //Thread.Sleep(6000);
            
            Console.WriteLine("工作到这里了..");

            Console.ReadKey();
            
        }

        static void back2_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            Product pp = e.Argument as Product;
            pp.age = 33;
            //e.Result = "我爱你..";
 
            //IAsyncResult iasynResult= actionDelegate.BeginInvoke(null, null);
            //AsyncResult r =iasynResult as AsyncResult;
            ThreadStart ts = new ThreadStart(delegate()
            {
                ValidateData(pp);
            });
            Thread thread = new Thread(ts);
            thread.IsBackground = true;
            thread.Start();
            while (thread.IsAlive)
            {
                if (worker.CancellationPending == true)
                {
                    //iasynResult.AsyncWaitHandle
                    //iasynResult.AsyncWaitHandle
                    Console.WriteLine("我被取消了...(验证数据..)");
                    try
                    {
                        thread.Abort();
                    }
                    catch (Exception)
                    {
                    }
                    e.Cancel = true;
                    break;
                }
                Console.WriteLine("我在轮询..(验证数据..)");
                Thread.Sleep(250);
            }
        }

        

        static void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BackgroundWorker back = sender as BackgroundWorker;
            if (e.Cancelled)
            {
                Console.WriteLine("结果被取消了..:"+e.Cancelled);
            }
            else
            {
                Console.WriteLine("结果:" + e.Result);
            }
           
        }

        static void back_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            BackgroundWorker back = sender as BackgroundWorker;
            Console.WriteLine("进度:" + e.ProgressPercentage + "%");
        }

        static void back_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            Product pp= e.Argument as Product;
            pp.age = 33;
            //e.Result = "我爱你..";

            Action actionDelegate = delegate()
            {
               e.Result=SaveFile();
            };
            //IAsyncResult iasynResult= actionDelegate.BeginInvoke(null, null);
            //AsyncResult r =iasynResult as AsyncResult;
             ThreadStart ts = new ThreadStart(delegate(){
                 e.Result = SaveFile();
             });
             Thread thread = new Thread(ts);
             thread.IsBackground = true;
             thread.Start();
             while (thread.IsAlive)
             {
                 if (worker.CancellationPending == true)
                 {
                     //iasynResult.AsyncWaitHandle
                     //iasynResult.AsyncWaitHandle
                     Console.WriteLine("我被取消了...");
                     try
                     {
                         thread.Abort();
                     }
                     catch (Exception)
                     {
                     }
                     e.Cancel = true;
                     break;
                 }
                 Console.WriteLine("我在轮询..");
                 Thread.Sleep(250);
             }
        }
        
        private static string SaveFile()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("我在保存文件.."+i);
                Thread.Sleep(1000);
            }
            return "ftp://adfadljkkjlk";

            //for (int i = 0; i < 10; i++)
            //{
            //    if (worker.CancellationPending == true)
            //    {
            //        e.Cancel = true;
            //        break;
            //    }
            //    Console.WriteLine(i);
            //    Thread.Sleep(1000);
            //    worker.ReportProgress((i + 1) * 10);
            //}
        }


        private static void ValidateData(object o)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("我在验证数据.."+i);
                Thread.Sleep(1000);
            }
        }


        public class Product
        {
            public string Name;
            public int age;
        }
    }
}
